package com.zx.dao.impl;

import com.zx.dao.DepartmentDao;
import com.zx.entity.Department;
import com.zx.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DepartmentDaoImpl implements DepartmentDao {
    @Override
    public List<Department> departmentList() {
        List<Department> departments = new ArrayList<>();
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "select * from t_department";
            PreparedStatement ps = connection.prepareStatement(sql);
            ResultSet resultSet = ps.executeQuery();
            while (resultSet.next()){
                int did = resultSet.getInt("did");
                String dname = resultSet.getString("dname");
                Department department = new Department(did,dname);
                departments.add(department);
            }
            return departments;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return departments;
    }
}
